//
//  LeftEyeViewController.m
//  安好
//
//  Created by qinlongqing on 14-8-26.
//  Copyright (c) 2014年 冯凯. All rights reserved.
//

#import "LeftEyeViewController.h"
#import "ShiLiTestingView.h"
#import "ShiLiTestViewController.h"


@interface LeftEyeViewController ()
{
    NSInteger leftEyeBestTestCharacterSize;
    NSMutableArray *testResultArray;
    
    CGFloat imageViewHeight;
    CGFloat titleLabelHeight;
    CGFloat goOnBTNHeight;
}
@end

@implementation LeftEyeViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        /*
        [[NSNotificationCenter defaultCenter] addObserverForName:@"nengKanQingChu" object:nil queue:nil usingBlock:^(NSNotification *note) {
            if (testResultArray.count >= 1) {
                NSDictionary *lastDict = [testResultArray lastObject];
                if ([[[lastDict allValues] lastObject] intValue] == 1) {//上次测试也能看见，这次的测试也能看见，所以要继续更小字号的测试
                    NSDictionary *dict = [NSDictionary dictionaryWithObject:[NSNumber numberWithInt:1] forKey:[NSString stringWithFormat:@"%d",leftEyeBestTestCharacterSize]];
                    [testResultArray addObject:dict];
                    
                    if (leftEyeBestTestCharacterSize <= 3) {//这么小的字都能看见，你的视力实在是太好了，不用测试了
                        [self.navigationController popViewControllerAnimated:NO];
#pragma 写入数据库相关操作 记录leftEyeBestTestCharacterSize
                        [self deallocNotification];
                        [self.shiLiTestViewController doRightEyeTestWithLeftEyeResult:[NSString stringWithFormat:@"%d",leftEyeBestTestCharacterSize]];
                    }
                    else{//更小字号的测试
                        [self.navigationController popViewControllerAnimated:YES];
                        leftEyeBestTestCharacterSize = leftEyeBestTestCharacterSize - 1;
                        [self doGoToTestBTN];
                    }
                }
                else{//上次测试看不清楚，这次测试能看清楚，所以最好的视力就是这次的测试，然后进行右眼的测试
                    [self.navigationController popViewControllerAnimated:NO];
#pragma 写入数据库相关操作 记录leftEyeBestTestCharacterSize
                    [self deallocNotification];
                    [self.shiLiTestViewController doRightEyeTestWithLeftEyeResult:[NSString stringWithFormat:@"%d",leftEyeBestTestCharacterSize]];
                }
            }
            else{
                NSDictionary *dict = [NSDictionary dictionaryWithObject:[NSNumber numberWithInt:1] forKey:[NSString stringWithFormat:@"%d",leftEyeBestTestCharacterSize]];
                [testResultArray addObject:dict];
                
                [self.navigationController popViewControllerAnimated:YES];
                leftEyeBestTestCharacterSize = leftEyeBestTestCharacterSize - 1;
                [self doGoToTestBTN];
            }
        }];
        [[NSNotificationCenter defaultCenter] addObserverForName:@"buNengQingChu" object:nil queue:nil usingBlock:^(NSNotification *note) {
            if (testResultArray.count >= 1) {
                NSDictionary *lastDict = [testResultArray lastObject];
                if ([[[lastDict allValues] lastObject] intValue] == 1) {//上次测试能看见，然后这次测试看不见，所以最好的视力就是上次的测试结果，然后进行右眼的测试
                    [self.navigationController popViewControllerAnimated:NO];
#pragma 写入数据库相关操作 记录[[lastDict allKeys] lastObject];
                    [self deallocNotification];
                    [self.shiLiTestViewController doRightEyeTestWithLeftEyeResult:[NSString stringWithFormat:@"%d",[[[lastDict allKeys] lastObject] intValue]]];
                }
                else{//上次测试看不见，这次测试也看不见，所以要继续更大字号的测试
                    NSDictionary *dict = [NSDictionary dictionaryWithObject:[NSNumber numberWithInt:0] forKey:[NSString stringWithFormat:@"%d",leftEyeBestTestCharacterSize]];
                    [testResultArray addObject:dict];
                    
                    if (leftEyeBestTestCharacterSize >= 30) {//你已无药可救了，这么大的字都看不清
                        [self.navigationController popViewControllerAnimated:NO];
#pragma 写入数据库相关操作 记录leftEyeBestTestCharacterSize
                        [self deallocNotification];
                        [self.shiLiTestViewController doRightEyeTestWithLeftEyeResult:[NSString stringWithFormat:@"%d",leftEyeBestTestCharacterSize]];
                    }
                    else{//更大字号的测试
                        [self.navigationController popViewControllerAnimated:YES];
                        leftEyeBestTestCharacterSize = leftEyeBestTestCharacterSize + 1;
                        [self doGoToTestBTN];
                    }
                }
            }
            else{
                NSDictionary *dict = [NSDictionary dictionaryWithObject:[NSNumber numberWithInt:0] forKey:[NSString stringWithFormat:@"%d",leftEyeBestTestCharacterSize]];
                [testResultArray addObject:dict];
                
                [self.navigationController popViewControllerAnimated:YES];
                leftEyeBestTestCharacterSize = leftEyeBestTestCharacterSize + 1;
                [self doGoToTestBTN];
            }
        }];
   */
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(doCanSee:) name:@"nengKanQingChu" object:nil];
        
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(doCannotSee:) name:@"buNengQingChu" object:nil];
    }
    return self;
}

- (void)doCanSee:(NSNotification *)notification{
    NSString *testSide = [notification.userInfo valueForKey:@"type"];
    if ([testSide isEqualToString:@"left"]) {
        if (testResultArray.count >= 1) {
            NSDictionary *lastDict = [testResultArray lastObject];
            if ([[[lastDict allValues] lastObject] intValue] == 1) {//上次测试也能看见，这次的测试也能看见，所以要继续更小字号的测试
                NSDictionary *dict = [NSDictionary dictionaryWithObject:[NSNumber numberWithInt:1] forKey:[NSString stringWithFormat:@"%ld",leftEyeBestTestCharacterSize]];
                [testResultArray addObject:dict];
                
                if (leftEyeBestTestCharacterSize <= 7) {//这么小的字都能看见，你的视力实在是太好了，不用测试了
                    [self.navigationController popViewControllerAnimated:NO];
#pragma 写入数据库相关操作 记录leftEyeBestTestCharacterSize
                    [self.shiLiTestViewController doRightEyeTestWithLeftEyeResult:[NSString stringWithFormat:@"%ld",leftEyeBestTestCharacterSize]];
                }
                else{//更小字号的测试
                    [self.navigationController popViewControllerAnimated:YES];
                    leftEyeBestTestCharacterSize = leftEyeBestTestCharacterSize - 1;
                    [self doGoToTestBTN];
                }
            }
            else{//上次测试看不清楚，这次测试能看清楚，所以最好的视力就是这次的测试，然后进行右眼的测试
                [self.navigationController popViewControllerAnimated:NO];
#pragma 写入数据库相关操作 记录leftEyeBestTestCharacterSize
                [self.shiLiTestViewController doRightEyeTestWithLeftEyeResult:[NSString stringWithFormat:@"%ld",leftEyeBestTestCharacterSize]];
            }
        }
        else{
            NSDictionary *dict = [NSDictionary dictionaryWithObject:[NSNumber numberWithInt:1] forKey:[NSString stringWithFormat:@"%ld",leftEyeBestTestCharacterSize]];
            [testResultArray addObject:dict];
            
            [self.navigationController popViewControllerAnimated:YES];
            leftEyeBestTestCharacterSize = leftEyeBestTestCharacterSize - 1;
            [self doGoToTestBTN];
        }
    }
}

- (void)doCannotSee:(NSNotification *)notification{
    NSString *testSide = [notification.userInfo valueForKey:@"type"];
    if ([testSide isEqualToString:@"left"]) {
        if (testResultArray.count >= 1) {
            NSDictionary *lastDict = [testResultArray lastObject];
            if ([[[lastDict allValues] lastObject] intValue] == 1) {//上次测试能看见，然后这次测试看不见，所以最好的视力就是上次的测试结果，然后进行右眼的测试
                [self.navigationController popViewControllerAnimated:NO];
#pragma 写入数据库相关操作 记录[[lastDict allKeys] lastObject];
                [self.shiLiTestViewController doRightEyeTestWithLeftEyeResult:[NSString stringWithFormat:@"%ld",[[[lastDict allKeys] lastObject] integerValue]]];
            }
            else{//上次测试看不见，这次测试也看不见，所以要继续更大字号的测试
                NSDictionary *dict = [NSDictionary dictionaryWithObject:[NSNumber numberWithInt:0] forKey:[NSString stringWithFormat:@"%ld",leftEyeBestTestCharacterSize]];
                [testResultArray addObject:dict];
                
                if (leftEyeBestTestCharacterSize >= 30) {//你已无药可救了，这么大的字都看不清
                    [self.navigationController popViewControllerAnimated:NO];
#pragma 写入数据库相关操作 记录leftEyeBestTestCharacterSize
                    [self.shiLiTestViewController doRightEyeTestWithLeftEyeResult:[NSString stringWithFormat:@"%ld",leftEyeBestTestCharacterSize]];
                }
                else{//更大字号的测试
                    [self.navigationController popViewControllerAnimated:YES];
                    leftEyeBestTestCharacterSize = leftEyeBestTestCharacterSize + 1;
                    [self doGoToTestBTN];
                }
            }
        }
        else{
            NSDictionary *dict = [NSDictionary dictionaryWithObject:[NSNumber numberWithInt:0] forKey:[NSString stringWithFormat:@"%ld",leftEyeBestTestCharacterSize]];
            [testResultArray addObject:dict];
            
            [self.navigationController popViewControllerAnimated:YES];
            leftEyeBestTestCharacterSize = leftEyeBestTestCharacterSize + 1;
            [self doGoToTestBTN];
        }        
    }
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationItem.title = @"视力";
    
    [self makeNavBar];
    
    if (kScreenWidth == 414) {
        imageViewHeight = 240;
        titleLabelHeight = 96;
        goOnBTNHeight = 54;
    }
    else{
        imageViewHeight = 200;
        titleLabelHeight = 80;
        goOnBTNHeight = 45;
    }
    
    CGFloat space = (kScreenHeight - 64 - imageViewHeight - titleLabelHeight - goOnBTNHeight)/4;
    

    testResultArray = [[NSMutableArray alloc]init];
    leftEyeBestTestCharacterSize = 13;
    
    [self.view setBackgroundColor:UIColorFromRGB(0x4fc1e9)];
    
    UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake((kScreenWidth - imageViewHeight)/2,  space, imageViewHeight, imageViewHeight)];
    imageView.image = [UIImage imageNamed:@"左眼视力测试"];
    [self.view addSubview:imageView];
    
    UILabel *titleLabel = [[UILabel alloc]init];
    if (kScreenWidth == 414) {
        titleLabel.frame = CGRectMake(20, imageView.frame.origin.y + imageView.frame.size.height + space, kScreenWidth - 40, titleLabelHeight);
        titleLabel.font = [UIFont systemFontOfSize:18];
    }
    else{
        titleLabel.frame = CGRectMake(15, imageView.frame.origin.y + imageView.frame.size.height + space, kScreenWidth - 30, titleLabelHeight);
        titleLabel.font = [UIFont systemFontOfSize:16];
    }
    titleLabel.textAlignment = NSTextAlignmentCenter;
    titleLabel.textColor = [UIColor whiteColor];
    titleLabel.text = @"遮住右眼,开始测试左眼";
    titleLabel.backgroundColor = [UIColor clearColor];
    [self.view addSubview:titleLabel];
    
    UIButton *goOnBTN = [[UIButton alloc]init];
    goOnBTN.layer.cornerRadius = 3;
    if (kScreenWidth == 414) {
        goOnBTN.frame = CGRectMake(20, titleLabel.frame.origin.y + titleLabel.frame.size.height + space, kScreenWidth - 40, goOnBTNHeight);
        goOnBTN.titleLabel.font = [UIFont systemFontOfSize:22];
    }
    else{
        goOnBTN.frame = CGRectMake(15, titleLabel.frame.origin.y + titleLabel.frame.size.height + space, kScreenWidth - 30, goOnBTNHeight);
        goOnBTN.titleLabel.font = [UIFont systemFontOfSize:20];
    }
    goOnBTN.backgroundColor = [UIColor whiteColor];
    [goOnBTN setTitleColor:UIColorFromRGB(0x4fc1e9) forState:UIControlStateNormal];
    [goOnBTN setTitle:@"开始测试" forState:UIControlStateNormal];
    [goOnBTN addTarget:self action:@selector(doGoToTestBTN) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:goOnBTN];
    
}

- (void)makeNavBar{
    UIBarButtonItem *backBtnImg = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"公用_返回.png"] style:UIBarButtonItemStylePlain target:self action:@selector(doBack)];
    [backBtnImg setTintColor:[UIColor whiteColor]];
    self.navigationItem.leftBarButtonItem = backBtnImg;
}

-(void)doBack{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)doGoToTestBTN{
    NSMutableArray *tempCharacters = [self.shiLiTestViewController randomCharacters];
    if (tempCharacters.count != 4) {
        return;
    }
    else{
        ShiLiTestingView *shiLiTestingView = [[ShiLiTestingView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight)];
        shiLiTestingView.backgroundColor = [UIColor whiteColor];
        shiLiTestingView.shiLiTestData = tempCharacters;
        shiLiTestingView.currentTestEyeViewController = self;
        shiLiTestingView.currentTestCharacterSize = leftEyeBestTestCharacterSize;
        [shiLiTestingView showShiLiTesting];
        [self.view addSubview:shiLiTestingView];
    }
}

- (void)dealloc{
    MyNSLog("leftViewController删除了通知");
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"nengKanQingChu" object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"buNengQingChu" object:nil];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
